
package action;

import beans.MessageBean;
import beans.ForumBean;
import db.Forum;
import java.util.ArrayList;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 * Show all forums in currnet page.
 * @author Tomas CASTA
 */
@UrlBinding("/index.action")
public class ShowAllForumsActionBean implements ActionBean {
    /** list of all forums */
    private ArrayList<ForumBean> list;
    /** action context */
    private ActionBeanContext context;
    /** message  */
    private String msg;
    /** count of forums */
    private int count;
    /** last message in forum */
    private MessageBean lastMesages;
    /** count of pages */
    private int countOfPages;
    /** current page */
    private int currentPage;
    /** next page */
    private int nextPage;
    /** previous page */
    private int previousPage;

    public MessageBean getLastMesages() {
        return lastMesages;
    }

    public void setLastMesages(MessageBean lastMesages) {
        this.lastMesages = lastMesages;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public ArrayList<ForumBean> getList() {
        return list;
    }

    public void setList(ArrayList<ForumBean> list) {
        this.list = list;
    }

    public void setCountOfPages(int countOfPages) {
        this.countOfPages = countOfPages;
    }

    public int getCountOfPages() {
        return countOfPages;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPreviousPage() {
        return previousPage;
    }

    public void setPreviousPage(int previousPage) {
        this.previousPage = previousPage;
    }

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    /**
     * Show all forums on current page.
     * @return Resolution
     */
    @DefaultHandler
    public Resolution show() {
        Forum forum = new Forum();
        forum.setDb(this.context.getServletContext());
        int page = 1;
        try {
           /* Parse page number from URI */
            if (context.getRequest().getParameter("page") != null) {
                page = Integer.valueOf(context.getRequest().getParameter("page"));
            }

            /* Show forums from current page */
            forum.showForum(page);

            /* Set pages */
            countOfPages = forum.getCountOfPages();
            currentPage = forum.getCurrentPage();
            nextPage = currentPage + 1;
            previousPage = currentPage - 1;
            
            /* Get forums to list */
            list = forum.getForumList();
            msg = forum.getMsg();
            }
        catch (NumberFormatException e) {
              return new ForwardResolution("index.action?page=1");
        }
        return new ForwardResolution("jsp/index.jsp");
    }
}
